package org.karelun.whiteboard.editparts;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.swt.widgets.Display;
import org.karelun.whiteboard.Triangle;
import org.karelun.whiteboard.WhiteboardPackage;
import org.karelun.whiteboard.gef.EditPolicy;
import org.karelun.whiteboard.gef.editparts.AbstractGraphicalEditPart;
import org.karelun.whiteboard.policy.DeleteShapeEditPolicy;

/**
 * @Author: KarelunHuang
 * @Date: Jan 23, 2013
 **/
public class TriangleEditPart extends AbstractGraphicalEditPart implements Adapter{

	private Notifier target;
	
	@Override
	protected IFigure createFigure() {
		org.eclipse.draw2d.Triangle triangle = new org.eclipse.draw2d.Triangle();
		triangle.setDirection(PositionConstants.NORTH);
		return triangle;
	}

	@Override
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new DeleteShapeEditPolicy());
	}
	
	@Override
	public void activate() {
		super.activate();
		((Triangle)getModel()).eAdapters().add(this);
	}
	
	@Override
	public void deactivate() {
		((Triangle)getModel()).eAdapters().remove(this);
		super.deactivate();
	}
	
	@Override
	public void notifyChanged(Notification notification) {
		int featureID = notification.getFeatureID(WhiteboardPackage.class);
		switch (featureID) {
		case WhiteboardPackage.SHAPE__X:
		case WhiteboardPackage.SHAPE__Y:
		case WhiteboardPackage.SHAPE__HEIGHT:
		case WhiteboardPackage.SHAPE__WIDTH:{
			Runnable runnable = new Runnable(){
				public void run() {
					refreshVisuals();
				}
			};
			Display.getDefault().asyncExec(runnable);
			break;
		}
		}
	}
	
	@Override
	protected void refreshVisuals() {
		Triangle shape=(Triangle)getModel();
		Dimension size = new Dimension(shape.getWidth(), shape.getHeight());
		Point point = new Point(shape.getX(),shape.getY());
		Rectangle rectangle = new Rectangle(point, size);
		((SheetEditPart)getParent()).setLayoutConstraint(this, getFigure(), rectangle);
		
		if(isLockedByOthers(shape)){
			getFigure().setForegroundColor(ColorConstants.red);
		}else if(isLockedBySelf(shape)){
			getFigure().setForegroundColor(ColorConstants.blue);
		}else{
			getFigure().setForegroundColor(ColorConstants.black);
		}
	}
	
	private boolean isLockedByOthers(EObject model) {
//		CDOLock cdoWriteLock = CDOUtil.getCDOObject(model).cdoWriteLock();
//		return cdoWriteLock.isLockedByOthers();
		return false;
	}
	
	private boolean isLockedBySelf(EObject model){
//		CDOLock cdoWriteLock = CDOUtil.getCDOObject(model).cdoWriteLock();
//		return cdoWriteLock.isLocked();
		return false;
	}
	
	@Override
	public Notifier getTarget() {
		return target;
	}

	@Override
	public void setTarget(Notifier newTarget) {
		this.target=newTarget;
	}

	@Override
	public boolean isAdapterForType(Object type) {
		return type.equals(getModel().getClass());
	}

}
